﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.DataModule;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;

namespace JKF.BusinessLayer.BLL
{
    public partial class bllSMS_CHARGE_LOG
    {
        public DataSet GetChargeLogByShop(string shopCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT [ID],B.SHOP_CODE,B.SHOP_NAME,A.[CHARGE_COUNT],C.ACCOUNT,CONVERT(CHAR(10),A.[DATE_CREATE],120) [DATE_CREATE],A.MEMO"
               +" FROM [SMS_CHARGE_LOG] A "
               +" INNER JOIN dbo.SHOP B ON  A.SHOP_CODE=B.SHOP_CODE "
               +" LEFT JOIN dbo.SYS_USER C ON A.USER_CREATE=C.USER_KEY "
               + " ");
            strSql.Append("   WHERE 1=1 and A.SHOP_CODE=@SHOP_CODE ");
            strSql.Append("   order by A.DATE_CREATE");
            var dbClass = DB.HR;
            DbCommand dbCommand = dbClass.GetSqlStringCommand(strSql.ToString());
            dbClass.AddInParameter(dbCommand, "SHOP_CODE", DbType.String, shopCode);

            return dbClass.ExecuteDataSet(dbCommand);
        }

        public bool SaveChargeLog(string shopCode,Int32 chargeCount,string memo)
        {
            var dbClass = DB.HR;
            var cmdList = new List<DbCommand>();

            try
            {
                //添加短信充值日志
                var strSql = "INSERT INTO [SMS_CHARGE_LOG](CHARGE_COUNT,SHOP_CODE,MEMO,USER_CREATE)"
                    + "VALUES(@CHARGE_COUNT,@SHOP_CODE,@MEMO,@USER_CREATE)";

                var cmd = dbClass.GetSqlStringCommand(strSql);
                dbClass.AddInParameter(cmd, "CHARGE_COUNT", DbType.Int32, chargeCount);
                dbClass.AddInParameter(cmd, "SHOP_CODE", DbType.String, shopCode);
                dbClass.AddInParameter(cmd, "MEMO", DbType.String, memo);
                dbClass.AddInParameter(cmd, "USER_CREATE", DbType.Guid, bllSYS_USER.CurrentUser.USER_KEY);
                cmdList.Add(cmd);


                //短信充值
                const string sql_SMS_ACCOUNT_update = "UPDATE  dbo.SMS_ACCOUNT SET AMOUNT=AMOUNT+@CHARGE_COUNT,NUM_LEFT=NUM_LEFT+@CHARGE_COUNT WHERE SHOP_CODE=@SHOP_CODE";
                var SMS_ACCOUNT_update_Cmd = dbClass.GetSqlStringCommand(sql_SMS_ACCOUNT_update);
                dbClass.AddInParameter(SMS_ACCOUNT_update_Cmd, "SHOP_CODE", DbType.String, shopCode);
                dbClass.AddInParameter(SMS_ACCOUNT_update_Cmd, "CHARGE_COUNT", DbType.Int32, chargeCount);
                cmdList.Add(SMS_ACCOUNT_update_Cmd);

                ExecTransaction.Exec(cmdList);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
